package cn.passlink.bletoothchat.Tools.UserId;


import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FriendsIDGenerator {
    public static String generateFriendsID(String phoneNumber, String name) throws NoSuchAlgorithmException {
        String input = phoneNumber + name;
        byte[] hash = getHash(input.getBytes(StandardCharsets.UTF_8));
        return bytesToHexString(hash).substring(0, 16);
    }

    private static byte[] getHash(byte[] input) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(input);
    }

    private static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}